<template>
  <div class="login-by-account">
    <el-form :model="form" :rules="rules" status-icon ref="formComponent">
      <el-form-item label="账号:" prop="account">
        <el-input v-model="form.account" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="form.password" type="password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
// 表单数据
const form = reactive({
  account: localStorage.getItem('account') || '',
  password: localStorage.getItem('password') || '',
})
// 表单校验规则
const rules = reactive({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
  ],
})
const formComponent = ref()
// 定义需要暴露给父组件的方法
const validateForm = () => {
  // 调用el-form的validate方法
  return formComponent.value.validate()
}
defineExpose({
  form,
  validateForm,
})
</script>

<style lang="scss" scoped></style>
